package com.fenghongzhang.day013;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    private Button send;
    private Button send2;
    private ImageView img;


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
        }
    };

    private LocalBroadcastManager manager;
    private MyReceiver myReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        send = (Button) findViewById(R.id.send);
        send2 = (Button) findViewById(R.id.send2);
        img = (ImageView) findViewById(R.id.img);


        //改成本地的.
        manager = LocalBroadcastManager.getInstance(this);

        //注册广播接收者
         myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.feng.1803");
        manager.registerReceiver(myReceiver,intentFilter);

        //注册2
        MyReceiver2 myReceiver2 = new MyReceiver2(handler, img);
        IntentFilter intentFilter2 = new IntentFilter();
        intentFilter2.addAction("com.bawei.1803");
        registerReceiver(myReceiver2,intentFilter2);
    }

    public void send(View view) {

        Intent intent = new Intent();
        intent.setAction("com.feng.1803");
        manager.sendBroadcast(intent);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        manager.unregisterReceiver(myReceiver);
    }

    public void send2(View view) {

        Intent intent = new Intent();
        intent.setAction("com.bawei.1803");
        sendBroadcast(intent);


    }
}
